package com.petecat.interchan.base.syscustom.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.petecat.interchan.base.syscustom.service.ICustomService;
import com.petecat.interchan.core.constans.Global;
import com.petecat.interchan.core.controller.BaseController;
import com.petecat.interchan.protocol.GlobalHeader;
import com.petecat.interchan.protocol.Result;
import com.petecat.interchan.protocol.base.custom.dto.SysCustomAddDTO;
import com.petecat.interchan.protocol.base.custom.dto.SysCustomUpdDTO;
import com.petecat.interchan.protocol.base.custom.vo.SysCustomVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@Api(value="自定义管理")
@RequestMapping("/base/custom")
public class CustomController extends BaseController{
	
	@Autowired
	private ICustomService customService;
	
	@ApiOperation(value = "新增User和项目信息", notes = "新增")
	@GetMapping(value = "/saveUserProject")
	public Result<?> saveUserProject(@RequestParam(value="ids",required=false) String[] ids,
			@ApiIgnore @RequestHeader(name = Global.GLOBAL_HEADER, required = false) String header) {
		GlobalHeader userInfo = this.getUserInfo(header, true);
		customService.saveUserProject(ids, userInfo.getUserId());
		return Result.ok();
	}

	@ApiOperation("删除User和项目信息")
	@DeleteMapping("/removeUserProjectById")
	public Result<?> removeUserProjectById(@RequestParam String peojectId,
			@ApiIgnore @RequestHeader(name = Global.GLOBAL_HEADER, required = false) String header) {
		GlobalHeader userInfo = this.getUserInfo(header, true);
		customService.removeUserProjectById(peojectId,userInfo.getUserId());
		return Result.ok();
	}
	

	@ApiOperation(value = "新增自定义配置信息", notes = "新增")
	@PostMapping(value = "/saveCustomConfig")
	public Result<?> saveCustomConfig(@RequestBody SysCustomAddDTO sysCustomAddDTO,
			@ApiIgnore @RequestHeader(name = Global.GLOBAL_HEADER, required = false) String header) {
		GlobalHeader userInfo = this.getUserInfo(header, true);
		customService.saveCustomConfig(sysCustomAddDTO, userInfo.getUserId());
		return Result.ok();
	}
	
	@ApiOperation(value = "修改自定义配置信息", notes = "修改")
	@PutMapping(value = "/updateCustomConfig")
	public Result<?> updateCustomConfig(@RequestBody SysCustomUpdDTO sysCustomAddDTO,
			@ApiIgnore @RequestHeader(name = Global.GLOBAL_HEADER, required = false) String header) {
		GlobalHeader userInfo = this.getUserInfo(header, true);
		customService.updateCustomConfig(sysCustomAddDTO, userInfo.getUserId());
		return Result.ok();
	}
	
	@SuppressWarnings("unchecked")
	@ApiOperation(value = "通过项目ID获取配置")
	@GetMapping(value = "/getCustomByProjectId")
	public Result<SysCustomVO> getCustomByProjectId(@RequestParam(value="projectId") String projectId,
			@ApiIgnore @RequestHeader(name = Global.GLOBAL_HEADER, required = false) String header) {
		GlobalHeader userInfo = this.getUserInfo(header, true);
		SysCustomVO customVO = customService.getCustomByProjectId(projectId, userInfo.getUserId());
		return (Result<SysCustomVO>) Result.ok(customVO);
	}

	
}
